﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcBlog.ViewModels;
using MvcBlog.Common.Models;

namespace MvcBlog.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {

        EntryDataAccess entryDB;
        CommentDataAccess commentDB;

        public HomeController()
        {
            var con = ConnectionFactory.getConnection();

            entryDB = new EntryDataAccess(con);
            commentDB = new CommentDataAccess(con);
        }



        public ActionResult Index()
        {
            int skip = 0;
            int take = 20;

            try
            {
                var viewModel = new EntryListViewModel()
                {

                    Entries = entryDB.getEntries(skip, take).ToList().Select(e => new EntryViewModel(e, commentDB.getCommentsByEntryID(e.Entry_ID))).ToList()

                };

                viewModel.SelectionStart = skip;
                viewModel.SelectionCount = take;

                return View(viewModel);

            }
            catch
            {
                return View("Error");
            }


        }

        public ActionResult About()
        {
            return View();
        }
    }
}
